//
//  TOTabBarController.m
//  SeaGullFM
//
//  Created by dllo on 15/11/22.
//  Copyright © 2015年 tianou. All rights reserved.
//

#import "WSTabBarController.h"
#import "WSCustomButton.h"
#import "WSPlayViewController.h"
//#import "TOPlayViewController.h"

@interface WSTabBarController ()

@property(nonatomic,retain)WSCustomButton *previousBtn;

@end

@implementation WSTabBarController

//自定义设置tabbar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController {
    //先删掉自己自定义的
    [self.tabBarImageView removeFromSuperview];
    
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        
    }else
    {
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        
        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if ([WSPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        
        [self.playButton startCircle];
    }
    if ([WSPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [self.playButton stopCircle];
        
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeImage:) name:@"changeImage" object:nil];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarImageView = [[WSBaseImageView alloc] init];
    self.tabBarImageView.userInteractionEnabled = YES;
    
    [super viewDidLoad];
    [self isHiddenTabBar:NO withViewController:nil];
    
    // 下面的方法是调用自定义的生成按钮的方法
    [self creatButtonWithNormalName:@"tabbar_find_n" andSelectName:@"tabbar_find_h" andTitle:@"发现" andIndex:0];
//    [self creatButtonWithNormalName:@"fount_n.png" andSelectName:@"fount_h.png" andTitle:@"发现" andIndex:0];
    
    
    [self creatButtonWithNormalName:@"tabbar_sound_n" andSelectName:@"tabbar_sound_h" andTitle:@"定制" andIndex:1];
    
    [self creatButtonWithNormalName:@"tabbar_category_n" andSelectName:@"tabbar_category_h" andTitle:@"分类" andIndex:3];
    
    [self creatButtonWithNormalName:@"tabbar_me_n" andSelectName:@"tabbar_me_h" andTitle:@"我的" andIndex:4];
    
    //默认选中第一个按钮
    WSCustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮
    
    
    self.playButton = [[WSTabBarPlayButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/5, SCREEN_WIDTH/5)];
    [self.tabBarImageView addSubview:self.playButton];
    [self.playButton.musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    
  
}

-(void)changeImage:(NSNotification *)notifition {
//    [self.playButton.musicBtn sd_setImageWithURL:notifition.object forState:UIControlStateNormal];
}



-(void)buttonAction:(UIButton *)btn {
    
    /*
    TOPlayViewController *player = [TOPlayViewController sharaMusicPlayer];
    TONavigationBarController *naVC = [[TONavigationBarController alloc] initWithRootViewController:player];
     
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
    }];
     */
}

#pragma mark 创建按钮通用方法
- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index {
    
    WSCustomButton *button = [WSCustomButton buttonWithType:UIButtonTypeCustom ];
    
    button. tag = index;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    button. frame = CGRectMake (SCREEN_WIDTH/5 * index, 0 , buttonW, buttonH);
    
    [button setImage :[UIImage imageNamed:normal] forState:UIControlStateNormal];
    
    [button setImage :[UIImage imageNamed:selected] forState : UIControlStateDisabled];//设置按钮无效时的图片样式
        
    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchUpInside];
//    
//    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
//    [button setTitle:title forState:UIControlStateNormal];
//    
//    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
//    
//    button.titleLabel.font = [ UIFont systemFontOfSize:12]; // 设置标题的字体大小

        
    [self.tabBarImageView addSubview:button];
    
}

#pragma mark 按钮被点击时调用

- ( void )changeViewController:(WSCustomButton *)sender {
    
    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
    
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if ( self.previousBtn != sender) {
        
        self.previousBtn.enabled = YES ;
        
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
